﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq.Expressions;
using GPS.MODELS;
using GPS.DataAccessLayer;


namespace GPS.Controllers
{
    public interface IGenericRepository<T> where T : class
    {
        IEnumerable<T> SelectAll();
        T SelectById(object id);
        void Insert(T obj, ApplicationUser initiator);
        void Update(T obj, ApplicationUser initiator);
        void Delete(object id);
        void Save();
    }
    public abstract class GenericRepository<C, T> : IGenericRepository<T>
        where T : class
        where C : DbContext, new()
    {
       
        protected DbSet<T> _table = null;

        private C _db = new C();
        public C Context
        {

            get { return _db; }
            set { _db = value; }
        }


        public GenericRepository(C _db)
        {
            this.Context = _db;
            _table = _db.Set<T>();
        }


        // select all
        public IEnumerable<T> SelectAll()
        {
            return _table.ToList();
        }

        // select by id
        public T SelectById(object id)
        {
            return _table.Find(id);
        }

        // insert
        public void Insert(T obj, ApplicationUser initiator)
        {
            _table.Add(obj);
        }

        // update
        public void Update(T obj, ApplicationUser initiator)
        {
            _table.Attach(obj);
            _db.Entry(obj).State = EntityState.Modified;
        }

        // delete
        public void Delete(object id)
        {
            T existing = _table.Find(id);
            _table.Remove(existing);
        }

        // save
        public void Save()
        {
            _db.SaveChanges();
        }

        public void Dispose()
        {
            _db.Dispose();
        }
    }


    public class GPS_DANHMUC_Repository<T> : GenericRepository<GPS.DataAccessLayer.GPS_NVContext, T> where T : class
    {
        public GPS_DANHMUC_Repository(GPS.DataAccessLayer.GPS_NVContext repository)
            : base(repository)
        {
        }
    }

    //public interface IRepository<T>
    //{
    //    void Add(T entity, ApplicationUser initiator);
    //    void Update(T entity, ApplicationUser initiator);
    //    void Delete(T entity);
    //    void Delete(int entityId);
    //    IEnumerable<T> GetAll();
    //    T GetById(int entityId);
    //}


    //public class GT_TOKHAIRepository : GenericRepository<GPS.DataAccessLayer.GPS_NVContext, GT_TOKHAI>, IGT_TOKHAIRepository
    //{

    //    public GT_TOKHAI GetSingle(int fooId)
    //    {

    //        var query = SelectAll().FirstOrDefault(x => x.ID_TO_KHAI == fooId);
    //        return query;
    //    }
    //}

    //public interface IGT_TOKHAIRepository : IGenericRepository<GT_TOKHAI>
    //{

    //    GT_TOKHAI GetSingle(int fooId);
    //}
   
}